﻿using System;
using System.Collections.Generic;
using System.Linq;
using TESTCommons.LinqUtil;
using TestDBData.EFContext;
using TestModel.SysManage;

namespace TestService.SysManage
{
    /// <summary>
    /// 系统用户
    /// </summary>
    public partial class Sys_RoleMenuFunctionService : ISys_RoleMenuFunctionService
    {
        #region 参数
        private readonly IRepository<Sys_Menu> _menuRepository;//菜单
        private readonly IRepository<Sys_Function> _FuncRepository;//功能
        private readonly IRepository<Sys_MenuFunction> _FunRepository;//菜单和功能
        private readonly IRepository<Sys_RoleMenuFunction> _RMFRepository;//角色；菜单；功能
        #endregion

        #region 构造函数
        public Sys_RoleMenuFunctionService(IRepository<Sys_Menu> menuRepository,
            IRepository<Sys_Function> FuncRepository,
            IRepository<Sys_MenuFunction> FunRepository,
            IRepository<Sys_RoleMenuFunction> RMFRepository)
        {
            _menuRepository = menuRepository;
            _FuncRepository = FuncRepository;
            _FunRepository = FunRepository;
            _RMFRepository = RMFRepository;
        }
        #endregion

        /// <summary>
        /// 保存
        /// </summary>
        public void SaveRoleMenuFunction(Guid RoleId, List<Sys_RoleMenuFunction> rmfModel)
        {
            _RMFRepository.Commit(() =>
            {
                var rmf = _RMFRepository.Table.Where(it => it.RoleMenuFunction_RoleID == RoleId);
                if (rmf != null && rmf.Count() > 0)
                {
                    _RMFRepository.Delete(rmf);
                }
                _RMFRepository.Insert(rmfModel);
            });
        }

        /// <summary>
        /// 菜单功能树
        /// </summary>
        public List<object> QueryRoleMenu(Guid RoleId)
        {
            var list = new List<object>();
            var _Sys_Menu_List = _menuRepository.Table.OrderBy(it => new { it.Menu_Num }).ToList();
            var _Sys_Function_List = _FuncRepository.Table.OrderBy(it => new { it.Function_Num }).ToList();
            var _Sys_MenuFunction_List = _FunRepository.Table.OrderBy(it => new { it.createTime }).ToList();
            var _Sys_RoleMenuFunction = _RMFRepository.Table.ToList();

            foreach (var item in _Sys_Menu_List)
            {
                list.Add(new
                {
                    id = item.gid,
                    name = item.Menu_Name + "(" + item.Menu_Num + ")",
                    pId = item.Menu_ParentID,
                    tag = "Menu",
                    @checked = false
                });
                //判断是否为末级菜单
                if (_Sys_Menu_List.Count(it => it.Menu_ParentID == item.gid) == 0)
                {
                    //遍历 菜单拥有的功能
                    var _SysMenuFunctionList = _Sys_MenuFunction_List.FindAll(it => it.MenuFunction_MenuID == item.gid);
                    foreach (var _MenuFunction in _SysMenuFunctionList)
                    {
                        //得到功能信息
                        var _Function = _Sys_Function_List.Find(it => it.gid == _MenuFunction.MenuFunction_FunctionID);

                        //判断该角色 对应的菜单和功能是否存在
                        var _Count = _Sys_RoleMenuFunction.Count(it =>
                          it.RoleMenuFunction_RoleID == RoleId &&
                          it.RoleMenuFunction_MenuID == item.gid &&
                          it.RoleMenuFunction_FunctionID == _MenuFunction.MenuFunction_FunctionID);

                        list.Add(new
                        {
                            id = _MenuFunction.MenuFunction_FunctionID,
                            name = _Function.Function_Name,
                            pId = item.gid,
                            tag = "MenuFunction",
                            @checked = _Count > 0
                        });
                    }
                }
            }
            return list;
        }

        public List<Sys_Menu> GetUserMenu(bool isManage, List<Guid> RoleIds)
        {
            var _Sys_Menu_List = _menuRepository.Table.Where(item=> item.Menu_IsShow !=2).ToList();
            if (isManage)
            {
                return _Sys_Menu_List; 
            }
            var _Sys_RoleMenuFunction = _RMFRepository.Table.ToList();
            return _Sys_RoleMenuFunction.Join(_Sys_Menu_List, rmf => rmf.RoleMenuFunction_MenuID, meun => meun.gid, (rmf, meun) => meun).ToList();
        }
    }
}
